package xx.iverson.sms_message.message;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;

/**
 * xx.iverson.sms_message.message.SendMessage
 * 发送短信服务实现类
 * on 2024/10/15
 *
 * @author weijx
 */
public class SendMessage {

    /**
     * 发送短信的主要业务逻辑
     *
     * @param phoneNumbers   目标手机号数组
     * @param templateId     短信模板ID
     * @param templateParams 模板参数数组
     * @throws TencentCloudSDKException 如果发送过程中发生错误
     */
    public static void sendSms(String[] phoneNumbers, String templateId, String[] templateParams) throws TencentCloudSDKException {
        // 初始化凭证
        Credential cred = new Credential("AKIDsdCbnmX5f2DBwUpzykJf5VmrpEXjkY8b", "OZWM0Qdu41kGf6zwlPtFPN9i5lLp9GGj");

        // 创建HTTP配置
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setReqMethod("POST");
        httpProfile.setConnTimeout(60);
        httpProfile.setWriteTimeout(60);
        httpProfile.setReadTimeout(60);
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        // 创建客户端配置
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);

        // 创建SMS客户端
        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);

        // 创建发送请求
        SendSmsRequest req = new SendSmsRequest();

        // 设置短信应用ID
        req.setSmsSdkAppId("1400595826");

        // 设置短信签名
        req.setSignName("奶茶少冰三分糖个人网");

        // 设置短信模板ID
        req.setTemplateId(templateId);

        // 设置短信模板参数
        req.setTemplateParamSet(templateParams);

        // 设置目标手机号码
        req.setPhoneNumberSet(phoneNumbers);

        // 发送短信
        SendSmsResponse res = client.SendSms(req);

        // 打印响应结果
        System.out.println(SendSmsResponse.toJsonString(res));
    }

    /**
     * 发送雨天提醒短信
     *
     * @param phoneNumbers 目标手机号数组
     * @throws TencentCloudSDKException 如果发送过程中发生错误
     */
    public static void sendSmsRain(String[] phoneNumbers,String [] templateParams) throws TencentCloudSDKException {
//        String[] templateParams = {"怡宝", "有雨", "带伞", "防滑"};
        sendSms(phoneNumbers, "1488144", templateParams);
    }

    /**
     * 发送天气预报短信
     *
     * @param phoneNumbers 目标手机号数组
     * @throws TencentCloudSDKException 如果发送过程中发生错误
     */
    public static void sendSmsWeather(String[] phoneNumbers) throws TencentCloudSDKException {
        String[] templateParams = {"77", "2024-10-15", "无风", "多云", "晴", "27℃", "东风", "3", "怡宝"};
        sendSms(phoneNumbers, "1488121", templateParams);
    }

    /**
     * 主入口方法，用于测试发送短信功能
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        // 测试参数
        String[] phoneNumbers = {"+8615210273807"};

        try {
            String[] templateParams = {"怡宝", "有雨", "带伞", "防滑"};
            // 调用发送雨天提醒短信方法
//            sendSmsRain(phoneNumbers,templateParams);

            // 调用发送天气预报短信方法
            sendSmsWeather(phoneNumbers);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
    }
}